﻿Imports System.Collections.ObjectModel
Imports System.Data.OleDb

Public Class MultipleChoiceQuestionModel
    Inherits QuestionModel
    Private _answers As List(Of MultipleChoiceAnswerModel)

    Public ReadOnly Property Answers As ReadOnlyCollection(Of MultipleChoiceAnswerModel)
        Get
            Return _answers.AsReadOnly()
        End Get
    End Property

    Public ReadOnly Property DataSet As DataSet
        Get
            Dim dSet As DataSet = New DataSet()
            Dim dTable As DataTable = dSet.Tables.Add("MultipleChoiceAnswers")

            Dim GUIDCol As DataColumn = New DataColumn("GUID", GetType(String))
            GUIDCol.ReadOnly = True
            GUIDCol.Unique = True
            dTable.Columns.Add(GUIDCol)

            Dim questionCol As DataColumn = New DataColumn("QuestionGUID", GetType(String))
            questionCol.ReadOnly = True
            dTable.Columns.Add(questionCol)

            Dim sequenceCol As DataColumn = New DataColumn("Sequence", GetType(Integer))
            sequenceCol.ReadOnly = True
            sequenceCol.Unique = True
            dTable.Columns.Add(sequenceCol)

            Dim answerCol As DataColumn = New DataColumn("Answer", GetType(String))
            answerCol.ReadOnly = True
            dTable.Columns.Add(answerCol)

            Dim correctCol As DataColumn = New DataColumn("Correct", GetType(Boolean))
            correctCol.ReadOnly = True
            dTable.Columns.Add(correctCol)

            dTable.PrimaryKey = New DataColumn() {GUIDCol}

            Dim dRow As DataRow
            For Each answer As MultipleChoiceAnswerModel In Answers
                dRow = dTable.NewRow()
                dRow("GUID") = answer.GUID
                dRow("QuestionGUID") = answer.QuestionGUID
                dRow("Sequence") = answer.Sequence
                dRow("Answer") = answer.Answer
                dRow("Correct") = answer.Correct
                dTable.Rows.Add(dRow)
            Next
            Return dSet
        End Get
    End Property

    Protected Overrides Sub CreateBlank()
        MyBase.CreateBlank()
        _answers = New List(Of MultipleChoiceAnswerModel)()
    End Sub

    Protected Overrides Function Load() As Boolean
        Dim IsInDB As Boolean
        Dim sqlString As String = _
            "SELECT q.Question, q.Sequence AS QuestionSequence, q.Type, q.QuizGUID, a.GUID AS AnswerGUID, a.Sequence as AnswerSequence, a.QuestionGUID, a.Answer, a.Correct " & _
            "FROM multipleChoiceAnswers a RIGHT OUTER JOIN questions q ON a.QuestionGUID = q.GUID " & _
            "WHERE  q.GUID = @GUID " & _
            "ORDER BY a.Sequence"
        Dim oleDbConn As OleDbConnection = db()
        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@GUID", GUID)
        Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
        IsInDB = dataReader.HasRows
        _answers = New List(Of MultipleChoiceAnswerModel)()
        If IsInDB Then
            dataReader.Read()
            InitialiseFromDataReader(dataReader)
            If Not IsDBNull(dataReader("QuestionGUID")) Then
                Do
                    _answers.Add(New MultipleChoiceAnswerModel(dataReader))
                Loop While dataReader.Read()
            End If
        End If
        oleDbConn.Close()
        Return IsInDB
    End Function

    Public Overrides Function Save() As Boolean
        ' Not Yet Implemented
        Return False
    End Function

    Public Sub New(ByVal newId As String)
        MyBase.new(newId)
    End Sub

End Class
